<?php

/**
 * UserLoginForm class.
 * UserLoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class UserLoginForm extends CFormModel
{

    public $email;
    public $password;
    public $authToken;
    public $rememberMe;
    private $_identity;

    const REMEMBER_DAY = 30;
    const REMEMBER_ME  = TRUE;

    /**
     * Declares the validation rules.
     * The rules state that email and password are required,
     * and password needs to be authenticated.
     */
    public function rules()
    {
        return array(
            // email and password are required
            array('email, password', 'required'),
            array('email', 'filter', 'filter' => 'trim'),
            // rememberMe needs to be a boolean
//            array('email', 'email'),
            array('rememberMe', 'boolean'),
            // password needs to be authenticated
            array('password', 'authenticate'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'email'      => Yii::t('user', 'Email'),
            'password'   => Yii::t('user', 'Password'),
            'rememberMe' => Yii::t('user', '<span>Remember me</span>'
            ),
        );
    }

    /**
     * Authenticates the password.
     * This is the 'authenticate' validator as declared in rules().
     */
    public function authenticate($attribute, $params)
    {
        $this->_identity = new UserIdentity($this->email, $this->password);
        if ($this->_identity->authenticate())
        {
            switch ($this->_identity->authenticate())
            {
                case CUserIdentity::ERROR_USERNAME_INVALID:
                    $this->addError('email', Yii::t('user', 'Incorrect email'));
                    break;
                case CUserIdentity::ERROR_PASSWORD_INVALID:
                    $this->addError('password', Yii::t('user', 'Email & Password does not match'));
                    break;
                case UserIdentity::ERROR_USER_NOT_ACTIVE:
                    $this->addError('email', Yii::t('user', 'Your account is not active'));
                    break;
            }
        }
    }

    /**
     * Logs in the user using the given email and password in the model.
     * @return boolean whether login is successful
     */
    public function login()
    {

        if ($this->_identity === null)
        {

            $this->_identity = new UserIdentity($this->email, $this->password, $this->authToken);
            $this->_identity->authenticate();
        }

        if ($this->_identity->errorCode === UserIdentity::ERROR_NONE)
        {

            $duration = 0; // 30 days            
            if (self::REMEMBER_ME)
            {
                $duration = $this->rememberMe ? 1000 * 60 * 60 * 24 * self::REMEMBER_DAY : 0;
            }
            Yii::app()->user->login($this->_identity, $duration);
            return true;
        }
        else
            return false;
    }

}
